package noticeboard;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.andro.chansung.ConstantId;
import com.andro.chansung.ConstantName;
import com.andro.chansung.ConstantURL;
import com.andro.chansung.DB;
import com.andro.chansung.R;

import freeboard.BoardUpdateThread;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

public class NoticeBoardService extends Service implements Runnable {
	private final static int TIME_UPDATE_PERIOD = 10 * 1000;
	
	private int startId;
	private boolean isRunning;
	private Runnable runnable;
	private NoticeBoardService context;
	private Handler handler;
	
	private List<Notice> tmpNotices;
	
	public void onCreate() {
		super.onCreate();
		context		= this;
		runnable 	= this;
		isRunning 	= false;
		handler		= new NoticeBoardServiceHandler();
		tmpNotices  = new ArrayList<Notice>();
	}
	
	public void onStart(Intent intent, int startId) {
		Log.d("debug", "Notice Board Service start ID = " + startId);
		super.onStart(intent, startId);
		this.startId = startId;
		
		if( !isRunning ) {
			handler.postDelayed(this, TIME_UPDATE_PERIOD);
			isRunning = true;
		}
	}

	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	public void onDestroy() {
		isRunning = false;
		super.onDestroy();
	}

	public void run() {
		if( !isRunning ) {
			Log.d("debug", "Notice Board Service run after destroy");
			return;
		} else {
			SQLiteDatabase db = openOrCreateDatabase(ConstantName.DATABASE_NAME, MODE_WORLD_READABLE, null);
			tmpNotices = DB.getNoticeFromNoticeTable(db);
			db.close();
			
			BoardUpdateThread thread = new BoardUpdateThread(ConstantURL.NOTICE_BOARD_URL, 1, this, handler);
			thread.start();
		}
	}

	public IBinder onBind(Intent intent) {
		return null;
	}

	private class NoticeBoardServiceHandler extends Handler {
		public void handleMessage(Message msg) {
			Log.d("debug", "hello there service?");
			SQLiteDatabase db = openOrCreateDatabase(ConstantName.DATABASE_NAME, MODE_WORLD_READABLE, null);
			List<Notice> notices = DB.getNoticeFromNoticeTable(db);
			db.close();
			
			if( notices.get(0).compareTo(tmpNotices.get(0)) > 0 ) {
				//new notice notification
				NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
				Notification notification 	= new Notification();
				notification.icon 			= R.drawable.ic_launcher;
				notification.tickerText 		= "새 게시물 '" + notices.get(0).getTitle() + "'";
				notification.when 			= new Date().getTime();
				
				notification.setLatestEventInfo(context, 
									"컴공 공지사항", 
									notification.tickerText, 
									PendingIntent.getActivity(context.getApplicationContext(), 
															  0, 
															  new Intent(context, NoticeBoardActivity.class), 
															  0)
									);
				
				notificationManager.notify(ConstantId.NOTICE_BOARD_NOTIFICATION_ID, notification);
			}
			handler.postDelayed(runnable, TIME_UPDATE_PERIOD);
		}
	}
}
